#include "StdAfx.h"
#include "OverlappingModulator.h"
#include "Statistics.h"

OverlappingModulator::OverlappingModulator(Modulator* modulator, unsigned int overlapLength)
{
	this->modulator = modulator;
	this->overlapLength = overlapLength;
}

OverlappingModulator::~OverlappingModulator(void)
{
}

void OverlappingModulator::Initialize(unsigned int windowLength)
{
	lastSample.clear();
	for(unsigned int i=0; i<windowLength; i++)
		lastSample.push_back(0);	
}

bool OverlappingModulator::NextModulatorSample(short* output, unsigned int length, std::vector<SampleMarker>* markersReceiver)
{
	if(lastSample.size()==0)
	{
		Initialize(length);
	}

	unsigned int outputIdx = 0;
	for(unsigned int i=lastSample.size()-overlapLength; i<lastSample.size(); outputIdx++, i++)
	{
		//beginning of output receives overlapping end of last sample
		output[outputIdx] = lastSample[i];
		//end of last sample becomes beginning of the next last sample
		lastSample[outputIdx] = lastSample[i];
	}

	bool retval = modulator->NextModulatorSample(&lastSample[overlapLength], length-overlapLength, markersReceiver);
	for(; outputIdx<length; outputIdx++)
	{
		output[outputIdx] = lastSample[outputIdx];
	}

	return retval;
}